#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int test0()
{
    //vector<int> numbers;//元素的个数为0
    //vector<int> numbers(10);//动态数组   10个0
    //vector<int> numbers(10, 1);//动态数组   10个1
    //vector<int> numbers{1, 2, 3, 4, 5};
    int arr[5] = {1, 2, 3, 4, 5};//C风格的数组写法
    vector<int> numbers(arr, arr + 5);//传递迭代器范围的写法
    for(int i = 0; i < numbers.size(); ++i) {
        cout << numbers[i] << " ";
    }
    cout << endl;
    cout << "numbers's size:" << numbers.size() << endl;

    return 0;
}

void test1()
{
    //动态扩容的策略：
    //当vector的空间不够的情况下，会自动进行扩容操作
    //具体的步骤：
    //1. 当size() 等于capacity() 时，要申请一个 2 * size()新空间
    //2. 复制原空间中的所有数据到新空间中
    //3. 释放原空间
    //4. 在新空间中最后一个元素的下一个位置添加新的数据
    vector<int> numbers;
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;
    numbers.push_back(1);//不扩容的情况下，时间复杂度为O(1)
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;
    numbers.push_back(1);
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;
    numbers.push_back(1);
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;
    numbers.push_back(1);
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;
    numbers.push_back(1);
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;

    numbers.shrink_to_fit();//释放掉数组中未使用的空间
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;

    numbers.clear();//清空数组中的所有元素, 但会保留空间
    cout << "numbers's size:" << numbers.size() << endl;
    cout << "numbers's capacity:" << numbers.capacity() << endl;

    //numbers对象本身占据的空间只有24个字节
    cout << "sizeof(numbers):" << sizeof(numbers) << endl << endl;

    vector<int> numbers2(1000);//直接创建1000个元素
    cout << "numbers2's size:" << numbers2.size() << endl;
    cout << "numbers2's capacity:" << numbers2.capacity() << endl;
    //numbers2对象本身占据的空间也只有24个字节
    cout << "sizeof(numbers2):" << sizeof(numbers2) << endl << endl;

    //需求：只开辟1000个元素的空间，暂时不存放元素
    vector<int> numbers3;
    numbers3.reserve(1000);//只开辟空间，不存放元素
    cout << "numbers3's size:" << numbers3.size() << endl;
    cout << "numbers3's capacity:" << numbers3.capacity() << endl;
}


void test2()
{
    vector<int> numbers{1, 2, 3, 4, 5};
    for(int i = 0; i < numbers.size(); ++i) {
        cout << numbers[i] << " ";
    }
    cout << endl;

    for(auto & e : numbers) {
        cout << e << " ";
    }
    cout << endl;

    //迭代器来访问元素
    //迭代器是一种类型, 目前来看可以将其认为就是指针
    vector<int>::iterator it = numbers.begin(); 
    cout << "*it:" << *it << endl;
    //end() : 指向的是最后一个元素的下一个位置, 该位置没有元素
    vector<int>::iterator it2 = numbers.end();
    cout << "*it2:" << *it2 << endl;//不确定的值
    cout << "*(it2 - 1):" << *(it2 - 1) << endl;
    ++it;
    it2--;
    cout << "*it:" << *it << endl;
    cout << "*it2:" << *it2 << endl;//
}

int main()
{
    /* test1(); */
    test2();
    return 0;
}

